package com.wannengbang.storemobile.homepage;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.base.BaseActivity;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.LoginBean;
import com.wannengbang.storemobile.bean.StoreListBean;
import com.wannengbang.storemobile.bean.UserInfoBean;
import com.wannengbang.storemobile.event.NewAddStoreEvent;
import com.wannengbang.storemobile.event.SwitchStoreEvent;
import com.wannengbang.storemobile.homepage.adapter.SelectStoreListAdapter;
import com.wannengbang.storemobile.homepage.model.HomePageModelImpl;
import com.wannengbang.storemobile.homepage.model.IHomePageModel;
import com.wannengbang.storemobile.login.LoginActivity;
import com.wannengbang.storemobile.utils.SPManager;
import com.wannengbang.storemobile.utils.ToastUtil;
import com.wannengbang.storemobile.widget.AppTitleBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SwitchStoreActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_add_store)
    LinearLayout llAddStore;
    private List<StoreListBean.DataBean.ItemListBean> beanList;
    private SelectStoreListAdapter listAdapter;
    private String store_no;
    private IHomePageModel homePageModel;
    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch_store);
        ButterKnife.bind(this);
        registerEventBus();
        initView();
        initData();
    }

    @Subscribe
    public void onNewAddStoreEvent(NewAddStoreEvent event) {
        initData();
    }

    private void initView() {
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        homePageModel = new HomePageModelImpl();
        beanList = new ArrayList<>();
        store_no = getIntent().getStringExtra("store_no");

        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        listAdapter = new SelectStoreListAdapter(beanList, store_no);
        recyclerView.setAdapter(listAdapter);

        listAdapter.setOnCallBackListener(position -> {
            StoreListBean.DataBean.ItemListBean itemListBean = beanList.get(position);
            requestChangeStore(itemListBean.getStore_no());
        });
    }

    public void requestChangeStore(String store_no) {
        homePageModel.requestChangeStore(store_no, new DataCallBack<LoginBean>() {
            @Override
            public void onSuccessful(LoginBean bean) {
                ToastUtil.showShort("切换成功");
                SPManager.getInstance().saveToken(bean.getData().getAccess_token());
                EventBus.getDefault().post(new SwitchStoreEvent());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 首页门店
     */
    public void initData() {
        homePageModel.requestStoreIndex(new DataCallBack<StoreListBean>() {
            @Override
            public void onSuccessful(StoreListBean bean) {
                beanList.clear();
                beanList.addAll(bean.getData().getItemList());
                listAdapter.notifyDataSetChanged();

                if (beanList.size() >= 6) {
                    llAddStore.setVisibility(View.GONE);
                } else {
                    llAddStore.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick(R.id.ll_add_store)
    public void onViewClicked() {
        if (userInfoBean == null) {
            return;
        }
        int agent_id = userInfoBean.getData().getAgent_id();
        Intent intent = new Intent(SwitchStoreActivity.this, IncomingVersionActivity.class);
        intent.putExtra("agent_id",agent_id);
        startActivity(intent);



    }
}
